package com.hwz.gulimall.product.service.impl;

import com.hwz.common.utils.Query;
import com.hwz.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.hwz.gulimall.product.entity.AttrEntity;
import com.hwz.gulimall.product.entity.CategoryEntity;
import com.hwz.gulimall.product.service.AttrAttrgroupRelationService;
import com.hwz.gulimall.product.service.AttrService;
import com.hwz.gulimall.product.service.CategoryService;
import com.hwz.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.hwz.gulimall.product.vo.SkuItemVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwz.common.utils.PageUtils;

import com.hwz.gulimall.product.dao.AttrGroupDao;
import com.hwz.gulimall.product.entity.AttrGroupEntity;
import com.hwz.gulimall.product.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    CategoryService categoryService;

    @Autowired
    AttrAttrgroupRelationService  attrAttrgroupRelationService;

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();

        Object object = params.get("catId");
        if (object != null){
            int catId = Integer.parseInt((String) params.get("catId"));
            if ( catId != 0){
                queryWrapper.eq("catelog_id", catId);
            }
        }

        String key = (String)params.get("key");
        if (StringUtils.isNotEmpty(key) && !"0".equals(key)){
            queryWrapper.and((obj)->{
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }


        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Long[] getCatIdPath(Long catId) {
        ArrayList<Long> list = new ArrayList<>();
        while (true){
            CategoryEntity categoryEntity = categoryService.getById(catId);
            list.add(catId);
            if (categoryEntity.getParentCid() == 0){
                break;
            }
            catId = categoryEntity.getParentCid();
        }
        Collections.reverse(list);
        return list.toArray(new Long[0]);
    }

    @Override
    public List<AttrGroupWithAttrsVo>  getAttrGroupWithAttrsByCatId(String cateLogId) {
        //查询关联的属性组
        List<AttrGroupEntity> attrGroupList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", cateLogId));
        if (CollectionUtils.isEmpty(attrGroupList)){
            return null;
        }
        //查询属性组关联的属性
        ArrayList<AttrGroupWithAttrsVo> res = new ArrayList<>();
        attrGroupList.forEach(attrGroupEntity -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsVo);
            //查询属性
            List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupEntity.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrs);
            res.add(attrGroupWithAttrsVo);
        });

        return res;
    }

    @Override
    public List<SkuItemVo.SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {

        List<SkuItemVo.SpuItemAttrGroupVo> attrGroupWithAttrsBySpuId = this.getBaseMapper().getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        return attrGroupWithAttrsBySpuId;
    }


}